﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using EFEntity;
using ISerives;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;

namespace Serives
{
    public class salary_grant_Servies : Isalary_grant_Servies
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;

        public salary_grant_Servies(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }
        public async Task<int> Add(Esalary_grant esg)
        {
            await myDbContext.Esg.AddAsync(esg);
            return myDbContext.SaveChanges();
        }

        public async Task<int> Del(int id)
        {
            myDbContext.Remove(myDbContext.Esg.Where(e => e.sgr_id == id).FirstOrDefault());
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<List<salary_grant>> Select()
        {
            List<salary_grant> list = new List<salary_grant>();
            await Task.Run(() =>
            {
                List<Esalary_grant> list2 = myDbContext.Esg.ToList();
                list = mapper.Map<List<salary_grant>>(list2);
            });
            return list;
        }

        public async Task<List<Esalary_grant>> SelectId(int id)
        {
            List<Esalary_grant> list = new List<Esalary_grant>();
            await Task.Run(() =>
            {
                List<Esalary_grant> list2 = myDbContext.Esg.Where(e => e.sgr_id == id).ToList();
                list = mapper.Map<List<Esalary_grant>>(list2);
            });
            return list;
        }

        public async Task<List<salary_grant>> SelectName(string name)
        {
            List<salary_grant> list = new List<salary_grant>();
            await Task.Run(() =>
            {
                List<Esalary_grant> list2 = myDbContext.Esg.Where(e => e.salary_standard_id == name).ToList();
                list = mapper.Map<List<salary_grant>>(list2);
            });
            return list;
        }
        /// <summary>
        /// 修改复核人
        /// </summary>
        /// <param name="esg"></param>
        /// <returns></returns>
        public async Task<int> Upd(Esalary_grant esg)
        {
          Esalary_grant e=myDbContext.Esg.Where(e => e.salary_grant_id == esg.salary_grant_id).FirstOrDefault();
            e.checker = esg.checker;
            e.check_time = esg.check_time;
            e.check_status = esg.check_status;
            return await myDbContext.SaveChangesAsync();
        }
        public async Task<List<Esalary_grant>> FenYe(FenYe fenye)
        {
            SqlParameter[] ps =
            {
                new SqlParameter(){ ParameterName="@pageSize",Value=fenye.PageSize},
                new SqlParameter(){ ParameterName="@keyName",Value="sgr_id"},
                new SqlParameter(){ ParameterName="@tableName",Value="salary_grant"},
                new SqlParameter(){ ParameterName="@where",Value=fenye.Where},
                new SqlParameter(){ ParameterName="@currentPage",Value=fenye.CurrentPage},
                new SqlParameter(){ ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType=SqlDbType.Int},
                new SqlParameter(){ ParameterName="@pages",Direction=ParameterDirection.Output,SqlDbType=SqlDbType.Int}
            };
            List<Esalary_grant> list = await myDbContext.Esg.FromSqlRaw("exec [dbo].[proc_FenYe] @pageSize, @keyName, @tableName, @where, @currentPage, @rows output, @pages output", ps).ToListAsync();
            fenye.Rows = (int)ps[5].Value;
            fenye.Pages = (int)ps[6].Value;
            //把EO-DTO
            List<Esalary_grant> list2 = new List<Esalary_grant>();
            foreach (Esalary_grant item in list)
            {
                Esalary_grant m = new Esalary_grant()
                {
                    sgr_id = item.sgr_id, //: 主键，自动增长列
                    salary_grant_id = item.salary_grant_id,// : 薪酬发放编号
                    salary_standard_id = item.salary_standard_id,// : 薪酬标准单编号
                    first_kind_id = item.first_kind_id,// : 一级机构编号
                    first_kind_name = item.first_kind_name, //: 一级机构名称
                    second_kind_id = item.second_kind_id,// : 二级机构编号
                    second_kind_name = item.second_kind_name,// : 二级机构名称
                    third_kind_id = item.third_kind_id,// : 三级机构编号
                    third_kind_name = item.third_kind_name,// : 三级机构名称
                    human_amount = item.human_amount,//: 总人数
                    salary_standard_sum = item.salary_standard_sum,//: 标准薪酬总金额
                    salary_paid_sum = item.salary_paid_sum, //: 实发总金额
                    register = item.register, //: 登记人
                    regist_time = item.regist_time, //: 登记时间
                    checker = item.checker, //: 复核人
                    check_time = item.check_time, //: 复核时间
                    check_status = item.check_status //: 复核状态
                };
                list2.Add(m);
            }
            return list2;
        }
    }
}
